//商家的禁用启用删除
layui.use('table', function () {
    var table = layui.table;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        // console.log(obj)
    });

    //方法级渲染，负责渲染表格数据
    table.render({
        elem: '#table_shop_list'
        ,method:'post'
        , url: '/shopCount'
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'aId', title: '商家ID', width: 100, sort: true, fixed: true}
            , {field: 'name', title: '商铺名', width: 150}
            , {field: 'address', title: '商铺地址', width: 150}
            , {field: 'license', title: '经营许可证', width: 250}
            , {
                field: 'createTime',
                title: '创建时间',
                with: 100,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
            // , {field: 'userEmail', title: '商家电话', sort: true, width: 250}
            , {
                field: 'status',
                title: '状态',
                width: 100,
                sort: true,
                templet: "<div>{{(d.status == 1) ? '审核通过':(d.status == 2)?'审核不通过':(d.status == 3)?'启用':(d.status == 4)?'禁用':(d.status == 5)?'删除':'待审核'}}</div>"
            }
            , {field: 'right', title: '操作', toolbar: '#barDemo', width: 250}
        ]]
        , id: 'testReload'
        , page: true
        , limit: 10
        , height: 460

    });
    //监听工具条
    table.on('tool(admin)', function (obj) {
        var data = obj.data;
        if (obj.event === 'forbidden') {

            $.ajax({
                    contentType: "application/json",
                    url: "/shopForbidden",
                    method: "post",
                    data: JSON.stringify({"aId": data.aId}),
                    dataType: "text",
                    success: function (data) {
                        if (data == "禁用成功") {
                            alert("禁用成功")
                            $(".layui-laypage-btn")[0].click();
                        } else {
                            alert("禁用失败")
                        }
                    },
                    error: function () {
                        alert("网络繁忙，正在尝试重新链接")
                    },
                }
            )
        } else if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {

                $.ajax({
                    contentType: "application/json",
                    url: "/adminDelete",
                    method: "post",
                    data: JSON.stringify({"aId": data.aId}),
                    dataType: "text",
                    success: function (data) {
                        if (data == "修改成功") {
                            alert("修改成功")
                            layer.closeAll();
                            $(".layui-laypage-btn")[0].click();
                        } else {
                            alert("修改失败")
                        }

                    },
                    error: function () {
                        alert("网络繁忙，正在尝试重新链接")
                    },
                })
            })
        } else if (obj.event === 'start') {

            $.ajax({
                    contentType: "application/json",
                    url: "/shopStart",
                    method: "post",
                    data: JSON.stringify({"aId": data.aId}),
                    dataType: "text",
                    success: function (data) {
                        if (data == "启用成功") {
                            alert("启用成功")
                            $(".layui-laypage-btn")[0].click();
                        } else {
                            alert("启用失败")
                        }
                    },
                    error: function () {
                        alert("网络繁忙，正在尝试重新链接")
                    },
                }
            )
        } else if (obj.event === 'pass') {
            var url = data.licenseImg;
            var shopName = data.name;
            var shopAddress = data.address;
            var shopLicense = data.license;
            var userName = data.userName;
            var aId = data.aId;

            layer.confirm(
                '<div class="layui-row">' +
                '<div class="layui-col-xs6">' +
                ' <div class="grid-demo grid-demo-bg1">' +
                '<table style="margin-left: 10px">' +
                    '<tr>' +
                        '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                        '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                        '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">商&nbsp&nbsp家&nbsp&nbsp名： ' + '</td>' +
                        '<td style="font-size: 25px">' + shopName + '</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                        '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 30px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                        '<td style="font-size: 30px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style=" font-size: 25px">商&nbsp家地&nbsp址： ' + '</td>' +
                        // '<td>&nbsp&nbsp&nbsp&nbsp</td>'+
                        '<td style="font-size: 25px">' + shopAddress + '</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                        '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                            '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                            '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">社会信用码： ' + '</td>' +
                        '<td style="font-size: 25px">' + shopLicense + '</td>' +
                    '</tr>' +
                    '<tr>' +
                            '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                            '<td style="font-size: 25px">&nbsp '+'</td>' +
                    '</tr>' +
                '<tr>' +
                    '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                    '<td style="font-size: 25px">&nbsp '+'</td>' +
                '</tr>' +
                    '<tr>' +
                        '<td style="font-size: 25px">法&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp人： ' + '</td>' +
                        '<td style="font-size: 25px">' + userName + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp '+'</td>' +
                '</tr>' +

                '<tr>' +
                    '<td>' + '<input type="button" class="layui-btn"  style="width: 80px" title="'+aId+'"  onclick="pass2(this)" value="通过">' + '</td>' +
                    '<td>' + '<input type="button" class="layui-btn"  style="width: 80px" title="'+aId+'"  onclick="noPass2(this)" value="不通过">' + '</td>' +
                '</tr>' +
                '</table>' +
                '</div>' +
                '</div>' +
                '<div class="layui-col-xs6">' +
                '<div class="grid-demo"><img width="400px" height="400px" src=' + url + '>' +
                '</div>' +
                '</div>' +
                '</div>'
                , {
                    title: '提示',
                    btn: ['取消'],
                    area: ['900px', '500px']
                }, function (index) {

                    // var newPwd = $("#newPwd").val();
                    // layer.closeAll();
                    layer.close(index);



                });

        }
    });


    //此处为重载方法，来自layui
    var $ = layui.$, active = {
        reload: function () {
            var shopName = $('#shopName');
            var shopStatus = $('#shopStatus').val();
            // alert("我执行过重载了");
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    name: shopName.val(),
                    status:shopStatus,
                }
            }, 'data');
        }
    };
    //此处方法来自layui，执行重载方法的部分
    $('#layerDemo .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});

function pass2(node) {
    // alert("进来了")
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    var aId= $(node).prop("title")
    // console.log(aId)
    $.ajax({
        // contentType: "application/json",
        url: "/shopCan",
        method: "post",
        data: "aId="+aId,
        dataType: "text",
        success: function (data) {
            if (data == "更新成功") {
                alert("更新成功")
                $(".layui-laypage-btn")[0].click();
                layer.closeAll();
            } else {
                alert("更新失败")
            }

        },
        error: function () {
            alert("网络繁忙，正在尝试重新链接")
        },
    })

}

function noPass2(node) {
    // alert("进来了")
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    var aId= $(node).prop("title")
    $.ajax({
        // contentType: "application/json",
        url: "/shopNoPass",
        method: "post",
        data: "aId="+aId,
        dataType: "text",
        success: function (data) {
            if (data == "更新成功") {
                alert("更新成功")
                $(".layui-laypage-btn")[0].click();
                layer.closeAll();
            } else {
                alert("更新失败")
            }

        },
        error: function () {
            alert("网络繁忙，正在尝试重新链接")
        },
    })

}

var $ = layui.$, active = {
    getCheckData: function () { //获取选中数据
        var checkStatus = table.checkStatus('idTest')
            , data = checkStatus.data;
        layer.alert(JSON.stringify(data));
    }
    , getCheckLength: function () { //获取选中数目
        var checkStatus = table.checkStatus('idTest')
            , data = checkStatus.data;
        layer.msg('选中了：' + data.length + ' 个');
    }
    , isAll: function () { //验证是否全选
        var checkStatus = table.checkStatus('idTest');
        layer.msg(checkStatus.isAll ? '全选' : '未全选')
    }
}


